package com.ht.baselibs.ui.base;

import android.content.Intent;
import android.text.TextUtils;
import android.webkit.WebSettings;

import com.ht.baselibs.R;
import com.ht.baselibs.ui.widget.jsbridge.BridgeHandler;
import com.ht.baselibs.ui.widget.jsbridge.BridgeWebView;

import java.util.Map;

/**
 * Created by huangtao on 16/10/19.
 */

public abstract class BaseWebViewActivity extends BaseActivity {

    public static final String URL = "url";
    public static final String TITLE = "title";

    private BridgeWebView mWebView;

    private String mUrl;
    private String mCenterTitle;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected void initIntentData(Intent callIntent) {
        mUrl = callIntent.getStringExtra(URL);
        mCenterTitle = callIntent.getStringExtra(TITLE);
    }

    @Override
    protected void initTitleBar() {
        if (!TextUtils.isEmpty(mCenterTitle)){
            mTitleBarView.setTitleText(mCenterTitle);
        }
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_base_webview;
    }

    @Override
    protected void initChildData() {
        mWebView.loadUrl(mUrl);
    }

    @Override
    protected void initChildView() {
        mWebView = (BridgeWebView)findViewById(R.id.activity_base_webview_webview);

        WebSettings settings = mWebView.getSettings();
        mWebView.setVerticalScrollBarEnabled(false);
        mWebView.setHorizontalScrollBarEnabled(false);
        settings.setJavaScriptEnabled(true);
        settings.setDomStorageEnabled(true);
    }

    @Override
    protected void setChildViewListener() {
        Map<String, BridgeHandler> handlerMap = getHandlers();
        registerHandlerHandlers(handlerMap);
    }

    private void registerHandlerHandlers(Map<String, BridgeHandler> handlerMap) {
        for (Map.Entry<String, BridgeHandler> entry : handlerMap.entrySet()){
            mWebView.registerHandler(entry.getKey(), entry.getValue());
        }
    }

    protected abstract Map<String, BridgeHandler> getHandlers();
}